package com.kitty.game.fight.book;

import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.fight.model.BattleUnit;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightAction;
import com.kitty.game.fight.util.FightMessageUtil;

/**
 * 
 * 反击
 * 受到物理攻击时，有一定几率物理反击
 *
 */
public class FanJiSkillProcessor extends BookSkillProcessor {

	@Override
	public Boolean effect(Fight fight, FightObject effector, FightObject target, Object... args) {
		if (!commonCheck(fight, effector, target)) {
			return false;
		}
		/**记录原始的攻击目标 和原始的动作 反击完成后需要设置回来*/
		int originalTargetId =effector.getTargetId();
		int originalAction = effector.getType();

		/**修改对象的攻击目标为反击的对象ID*/
		effector.setTargetId(target.getId());
		/**修改动作为反击的动作*/
		effector.setType(FightAction.COUNTER_ATTACK);


		BattleUnit battleUnit = new BattleUnit(fight, effector, target);
		battleUnit.setBackAttack();
		battleUnit.triggerPhysic();

		FightMessageService fightMessageService = SpringUtils.getFightMessageService();
		if (target.isFallDown()) {
			FightMessageUtil.sendFightPackage(fight, fightMessageService.fallDown(target.getId(), 1));
			fight.onActorDie(target);
		}

		/**还原回来反击之前的目标和动作*/
		effector.setTargetId(originalTargetId);
		effector.setType(originalAction);

		FightMessageUtil.sendFightPackage(fight, fightMessageService.doActionEnd(effector.getId()));

		return true;
	}

}
